<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Reports extends CI_Controller {

	public $data = array();

	function __construct()
	{
		parent::__construct();

		if( !$this->session->userdata('logged_in') )
			redirect('login');

		$this->load->model('merchant_model');
		$this->load->config('regions');
		$this->load->library('form_validation');
		$this->load->library('curl');
		$this->load->helper('form');
		$this->load->helper('download');
	}

		
	function index( $mid = 0 )
	{
		$mid = (int)$mid;

		if( !$mid )
		{
			redirect('reports/merchant');
		}

		//get merchant details
		$merchant_details = $this->merchant_model->get_by_id( $mid );

		if( !count($merchant_details) )
		{
			redirect('reports/merchant');
		}

		$this->data['merchant_details'] = $merchant_details;
		$this->data['next_token'] = '';
		$this->data['records'] = array();
		$limit = 50;
		$this->data['limit'] = $limit;
		
		$this->data['days'] = 90;
		$this->data['report_type'] = '';
		
		
		$this->data['reports'] = $this->config->item('reports');
		
		$report_count_url = site_url('api/reports/reportCount?KEY='.$merchant_details['merchant_id']);
		$report_list_url = site_url('api/reports/reportList/format/json?MaxCount='.$limit.'&KEY='.$merchant_details['merchant_id']);
		
		if( isset($_POST['next_token']) )
		{
			$report_list_url = site_url('api/reports/reportListByNextToken/format/json?NextToken='.urlencode($_POST['next_token']).'&KEY='.$merchant_details['merchant_id']);
		}
		else 
		{
			if( isset($_POST['report_type']) )
			{
				$report_list_url .= '&ReportTypeList='.$_POST['report_type'];
				$this->data['report_type'] = $_POST['report_type'];
			}
			if( isset($_POST['days']) )
			{
				$report_list_url .= '&AvailableFromDate='.$_POST['days'];
				$this->data['days'] = $_POST['days'];
			}			
			
		}

		$count = $this->get_api_data( $report_count_url );
		$this->data['count']		= $count;

		$result = $this->get_api_data( $report_list_url );
		$result = json_decode($result, TRUE);
		
		if(isset($result['next_token']))
		{
			$this->data['next_token'] 	= $result['next_token'];
		}
		
		if( isset($result['GetReportListResult']) )
		{
			$result = current($result['GetReportListResult']);
			if( isset($result['ReportInfo']) ){

				if( isset($result['ReportInfo'][0]))		
					$this->data['records']		= $result['ReportInfo'];
				else
					$this->data['records'][0]	= $result['ReportInfo'];	 
			}	
		}


		$this->layout->view('reports/list', $this->data);
	}


	function merchant()
	{
		$this->data['error'] = '';
	
		//get Merchants List
		$result = $this->merchant_model->get_all();
	
		$this->data['merchants'] = array();
		$this->data['merchants'][''] = 'select';
		foreach ($result['rows'] as $row)
		{
			$this->data['merchants'][$row['id']] = $row['name'];
		}
	
	
		$this->form_validation->set_rules('merchant_id', 'Merchant', 'required');
	
		if($this->form_validation->run())
		{
			$merchant_id = $this->input->post('merchant_id');
	
			redirect('reports/'.$merchant_id);
		}
	
		$this->layout->view('reports/view', $this->data);
	}
	

	function get_report( $report_id = '' , $mid = '' )
	{
		$mid = (int)$mid;
		
		if( !$mid )
		{
			redirect('reports/merchant');
		}
		
		//get merchant details
		$merchant_details = $this->merchant_model->get_by_id( $mid );
		
		if( !count($merchant_details) )
		{
			redirect('reports/merchant');
		}
		
		$this->data['merchant_details'] = $merchant_details;
		
		$report_url = site_url("api/reports/report/format/json?ReportId={$report_id}&KEY={$merchant_details['merchant_id']}");
		//die($report_url);
		$result = $this->get_api_data( $report_url );
		
		$result = json_decode($result, TRUE);
		
		if( isset($result['filePath']) )
		{
			$tmp = explode('/',$result['filePath']);
			force_download(end($tmp), file_get_contents($result['filePath']));
		}
		else
		{
			force_download('eeror.txt', 'There was an Error in generating report.');
		}
		
		
		
	}
	
	function get_api_data( $url = '')
	{
		$this->curl->create($url);
        $this->curl->option('SSL_VERIFYPEER', false); // for ssl
        $this->curl->option('RETURNTRANSFER', true);
        $data = $this->curl->execute();
		
        return $data;
        
	}

	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
